package org.sam.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.util.Collection;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableCellRenderer;

import org.sam.swing.table.JSTable;
import org.sam.swing.table.JSTableBuilder;
import org.sam.swing.table.JSTableColumn;
import org.sam.swing.table.JSTableColumnModel;
import org.sam.swing.table.JSTableModel;
import org.sam.swing.table.defaultImpl.JSTableArrayBuilderImpl;
import org.sam.swing.table.tools.JSTableToolBar;

/**
 * 测试数组格式的窗口
 * @author sam
 *
 */
public class JFrameArrayTable extends JSFrame {

	private static final long serialVersionUID = 5117463073766725700L;
	
	/**
	 * 当前的table
	 */
	private JSTable table;

	/**
	 * tablemodel
	 */
	private JSTableModel<Collection<Object[]>> tableModel;

	/**
	 * colmodel
	 */
	private JSTableColumnModel tableColModel;
	
	public JFrameArrayTable() {
		super();
		initCompents();
	}

	/**
	 * 初始化窗口
	 */
	protected void initCompents() {
		DefaultTableCellRenderer renderl = new DefaultTableCellRenderer();
		renderl.setHorizontalAlignment(DefaultTableCellRenderer.LEFT);

		JSTableColumn c0 = new JSTableColumn();
		c0.setHeaderValue("时间");
		c0.setTitle("时间");
		c0.setIdentifier(0);
		c0.setCellRenderer(renderl);
		c0.setWidth(15);
		c0.setModelIndex(0);

		JSTableColumn c1 = new JSTableColumn();
		c1.setHeaderValue("航空器1");
		c1.setTitle("航空器1");
		c1.setIdentifier(1);
		c1.setCellRenderer(renderl);
		c1.setMinWidth(75);
		c1.setDefaultValue(null);

		JSTableColumn c1_1 = new JSTableColumn();
		c1_1.setHeaderValue("经度1");
		c1_1.setTitle("经度1");
		c1_1.setIdentifier(2);
		c1_1.setCellRenderer(renderl);
		c1_1.setMinWidth(75);
		c1_1.setDefaultValue(null);

		JSTableColumn c1_2 = new JSTableColumn();
		c1_2.setHeaderValue("纬度1");
		c1_2.setTitle("纬度1");
		c1_2.setIdentifier(3);
		c1_2.setCellRenderer(renderl);
		c1_2.setMinWidth(75);
		c1_2.setDefaultValue(null);

		JSTableColumn c1_3 = new JSTableColumn();
		c1_3.setHeaderValue("高度1");
		c1_3.setTitle("高度1");
		c1_3.setIdentifier(4);
		c1_3.setCellRenderer(renderl);
		c1_3.setMinWidth(75);
		c1_3.setDefaultValue(null);

		JSTableColumn c1_4 = new JSTableColumn();
		c1_4.setHeaderValue("速度1");
		c1_4.setTitle("速度1");
		c1_4.setIdentifier(5);
		c1_4.setCellRenderer(renderl);
		c1_4.setMinWidth(75);
		c1_4.setDefaultValue(null);

		JSTableColumn c2 = new JSTableColumn();
		c2.setHeaderValue("航空器2");
		c2.setTitle("航空器2");
		c2.setIdentifier(6);
		c2.setCellRenderer(renderl);
		c2.setMinWidth(75);
		c2.setDefaultValue(null);

		JSTableColumn c2_1 = new JSTableColumn();
		c2_1.setHeaderValue("经度2");
		c2_1.setTitle("经度2");
		c2_1.setIdentifier(7);
		c2_1.setCellRenderer(renderl);
		c2_1.setMinWidth(75);
		c2_1.setDefaultValue(null);

		JSTableColumn c2_2 = new JSTableColumn();
		c2_2.setHeaderValue("纬度2");
		c2_2.setTitle("纬度2");
		c2_2.setIdentifier(8);
		c2_2.setCellRenderer(renderl);
		c2_2.setMinWidth(75);
		c2_2.setDefaultValue(null);

		JSTableColumn c2_3 = new JSTableColumn();
		c2_3.setHeaderValue("高度2");
		c2_3.setTitle("高度2");
		c2_3.setIdentifier(9);
		c2_3.setCellRenderer(renderl);
		c2_3.setMinWidth(75);
		c2_3.setDefaultValue(null);

		JSTableColumn c2_4 = new JSTableColumn();
		c2_4.setHeaderValue("速度2");
		c2_4.setTitle("速度2");
		c2_4.setIdentifier(10);
		c2_4.setCellRenderer(renderl);
		c2_4.setMinWidth(75);
		c2_4.setDefaultValue(null);

		JSTableColumn c3 = new JSTableColumn();
		c3.setHeaderValue("距离");
		c3.setTitle("距离");
		c3.setIdentifier(11);
		c3.setCellRenderer(renderl);
		c3.setMinWidth(75);
		c3.setDefaultValue(null);
		
		JSTableColumn c4 = new JSTableColumn();
		c4.setHeaderValue("高度差");
		c4.setTitle("高度差");
		c4.setIdentifier(12);
		c4.setCellRenderer(renderl);
		c4.setMinWidth(75);
		c4.setDefaultValue(null);

		JSTableColumn c5 = new JSTableColumn();
		c5.setHeaderValue("系数");
		c5.setTitle("系数");
		c5.setIdentifier(13);
		c5.setCellRenderer(renderl);
		c5.setMinWidth(75);
		c5.setDefaultValue(null);

		try {

			JSTableBuilder<Collection<Object[]>> builder = new JSTableArrayBuilderImpl(c0, c1, c1_1, c1_2, c1_3, c1_4,
					c2, c2_1, c2_2, c2_3, c2_4, c3, c4,c5);
			this.tableModel = builder.buildTableModel();
			this.tableColModel = builder.buildTableColumnModel();
			this.table = builder.buildTable();

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		this.setLayout(new BorderLayout());
		this.add(new JScrollPane(this.table), BorderLayout.CENTER);
		this.add(buildToolBar(), BorderLayout.NORTH);
		
	}
	
	/**
	 * 生成工具條
	 * @return
	 */
	protected JSToolBar buildToolBar() {
		JSTableToolBar toolBar = new JSTableToolBar(table);
		toolBar.enableRetrieve();
		toolBar.enableEdit();
		toolBar.enableAdd();
		toolBar.enableDel();
		toolBar.enableSave();
		
		Action action1 = new AbstractAction() {
			
			private static final long serialVersionUID = 7388714928382953046L;

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Object[] value = new Object[] { table.getRowCount() , "", 0.0,
							0.0, 100, 0.0, 0, 0.0, 0.0,
							100.0, 200.0, "dfdfdfd", 1,
							0.5 };
					tableModel.insert(0,value);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		};
		action1.putValue(Action.SHORT_DESCRIPTION, "插入數據測試");
		toolBar.addAction(action1);
		
		Action action = new AbstractAction() {
			
			private static final long serialVersionUID = 7388714928382953046L;

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Collection<Object[]> datas = tableModel.getDatas();
					datas.forEach(n -> {
						System.out.println(n[0]);
					});
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		};
		action.putValue(Action.SHORT_DESCRIPTION, "獲取數據測試");
		toolBar.addAction(action);
		return toolBar;
	}
}
